package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.redis.RedisKey;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.dto.CIndustryTreeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CIndustryMapper;
import com.ruoyi.system.domain.CIndustry;
import com.ruoyi.system.service.ICIndustryService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-01-11
 */
@Service
public class CIndustryServiceImpl implements ICIndustryService 
{
    @Autowired
    private CIndustryMapper cIndustryMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public CIndustry selectCIndustryById(Long id)
    {
        return cIndustryMapper.selectCIndustryById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param cIndustry 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<CIndustry> selectCIndustryList(CIndustry cIndustry)
    {    //cIndustry空的时候走缓存
        if(cIndustry==null || cIndustry.getId()==null){
            List<CIndustry> list= redisCache.getCacheList(RedisKey.GEO_SYSTEM_INDUSTRY);
            if(list.isEmpty()){
                return cIndustryMapper.selectCIndustryList(cIndustry);
            }else{
                return list;
            }
        }
        return cIndustryMapper.selectCIndustryList(cIndustry);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param cIndustry 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertCIndustry(CIndustry cIndustry)
    {
        cIndustry.setCreateTime(DateUtils.getNowDate());
        return cIndustryMapper.insertCIndustry(cIndustry);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param cIndustry 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateCIndustry(CIndustry cIndustry)
    {
        cIndustry.setUpdateTime(DateUtils.getNowDate());
        return cIndustryMapper.updateCIndustry(cIndustry);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteCIndustryByIds(Long[] ids)
    {
        return cIndustryMapper.deleteCIndustryByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteCIndustryById(Long id)
    {
        return cIndustryMapper.deleteCIndustryById(id);
    }


    @Override
    public List<CIndustryTreeDTO> getCIndustryTree() {
        List<CIndustryTreeDTO> cIndustryTree =new ArrayList<>();
        List<CIndustryTreeDTO> firstIndustry = cIndustryMapper.getFirstIndustry();
        List<CIndustryTreeDTO> secondIndustry = cIndustryMapper.getSecondIndustry();
        Map<String, List<CIndustryTreeDTO>> collect = secondIndustry.stream().collect(Collectors.groupingBy(CIndustryTreeDTO::getParentId));
        for (CIndustryTreeDTO cIndustry : firstIndustry
             ) {
            cIndustry.setcIndustryList(collect.get(cIndustry.getIndustryCode()));
            cIndustryTree.add(cIndustry);
        }
        return cIndustryTree;
    }
}
